/**
 * EnterpriseAssets
 */
jsx3.lang.Package.definePackage("tibco.admin.enterpriseAssets", function (eap) {
});

/**
 * EnterpriseAssets
 */
jsx3.lang.Class.defineClass("tibco.admin.enterpriseAssets.EnterpriseAssets",
        jsx3.lang.Object,
        [tibco.admin.plugin.SelectPerspectiveInterface, tibco.admin.plugin.PluginContainerInterface],
        function (plugin) {
            plugin.instance = null;
            plugin.prototype.tabsToLoad = new Object();
            plugin.prototype.allTabs = new Object();
            plugin.prototype.tabbedPane = null;

            plugin.prototype.init = function() {
                this.jsxsuper();
            }

            plugin.prototype.getNamespace = function() {
                return "tibco.admin";
            }

            plugin.prototype.getSectionTitle = function(id) {
                return "Enterprise Assets";
            }

            plugin.prototype.getSecondarySelectionText = function() {
                return "";
            }

            plugin.prototype.destroyPlugin = function() {
            }

            plugin.prototype.initPlugin = function() {
                //load dynamic properties for labels
                var loaded = tibco.admin.Main.loadResource("tibco/admin/enterpriseAssets/jss/locale.xml", "tibco.admin", null, "ljss")
            }

            plugin.prototype.onAfterComponentUnloaded = function() {
                this.instance = null;
                this.tabsToLoad = new Object();
                this.allTabs = new Object();
                this.tabbedPane = null;
            }

            plugin.getInstance = function(args) {
                if(!this.instance) {
                    this.instance = new tibco.admin.enterpriseAssets.EnterpriseAssets();
                }
                return this.instance;
            }

            plugin.prototype.getIconPath = function() {
                return tibco.admin.Main.getServer().getDynamicProperty("@addin@Welcome EA Icon");
            }

            plugin.prototype.getDescription = function() {
                return tibco.admin.Main.getServer().getDynamicProperty("@@welPageCfgEntContent_00");
            }

            plugin.prototype.getSelectText = function() {
                return tibco.admin.Main.getServer().getDynamicProperty("@@ConfigureEnterpriseAssets_20")
            }

            plugin.prototype.loadComponent = function(block) {
                this.notifyParent(null);
            }

            plugin.prototype.getComponentPath = function() {
                return "tibco/admin/enterpriseAssets/component.xml"
            }

            plugin.prototype.loadStage2Component = function(parent, id) {
                this.tabbedPane = tibco.admin.Main.loadUIResource(parent, "tibco.admin", "tibco/admin/enterpriseAssets/components/tabPaneEnterpriseAsset.xml", true);
                this.tabbedPane.setLabelText(this.getSelectText())
                var me = this;
                this.tabbedPane.subscribe(com.gix.gui.PerspectiveTabbedPane.TAB_SELECTED, me, me.loadTab)
                this.loadDetails();
            }

            plugin.prototype.onAfterGetPlugins = function() {
                var iter = this.getPluginList("tibco.admin.enterpriseAssets.EA01").iterator();
                var first = true;
                var tabtext = null;
                var isloaded = true;
                while(iter.hasNext()) {
                    var obj = iter.next();
                    tabtext = obj.getTabText()
                    var tab = this.tabbedPane.addTab(tabtext);
                    if(first) {
                        obj.loadComponent(tab.getContentPane());
                        first = false;
                        this.allTabs['tab' + tabtext] = obj;
                    }
                    else {
                        this.tabsToLoad['tab' + tabtext] = obj;
                        this.allTabs['tab' + tabtext] = obj;
                    }
                }
            }

            plugin.prototype.setupPlugin = function(obj) {
            }

            plugin.prototype.loadTab = function(objEvent) {
                var name = objEvent.target.getText();
                var obj = this.tabsToLoad['tab' + name];
                if(obj) {
                    obj.loadComponent(objEvent.target.getContentPane());
                    delete this.tabsToLoad['tab' + name];
                }
                else {
                    obj = this.allTabs['tab' + name];
                    if(obj) {
                        setTimeout(function() {
                            obj.tabSelected();
                        }, 0);
                    }
                }
            }

            plugin.prototype.loadDetails = function() {
                if(this.getPluginList().size() == 0) {
                    var preLoad = new jsx3.util.List(0);

                    // load Environment def
                    if(!tibco.admin.enterpriseAssets.envDefPlugin || !tibco.admin.enterpriseAssets.envDef.EnvDefPlugin) {
                        try {
                            tibco.admin.Main.loadClass("tibco/admin/enterpriseAssets/envDef/EnvDefPlugin.js", "tibco.admin");
                            preLoad.add(tibco.admin.enterpriseAssets.envDef.EnvDefPlugin, preLoad.size());
                        }
                        catch(ex) {
                            tibco.admin.Main.logException(ex);
                        }
                    }
                    else {
                        preLoad.add(tibco.admin.enterpriseAssets.envDef.EnvDefPlugin, preLoad.size());
                    }

                    // load Machines tab
                    if(!tibco.admin.enterpriseAssets.machines || !tibco.admin.enterpriseAssets.machines.Machines) {
                        try {
                            tibco.admin.Main.loadClass("tibco/admin/enterpriseAssets/machines/Machines.js", "tibco.admin");
                            preLoad.add(tibco.admin.enterpriseAssets.machines.Machines, preLoad.size());
                        }
                        catch(ex) {
                            tibco.admin.Main.logException(ex);
                        }
                    }
                    else {
                        preLoad.add(tibco.admin.enterpriseAssets.machines.Machines, preLoad.size());
                    }

                    // Load shared resource definitions tab
                    if(!tibco.admin.enterpriseAssets.shdResDefs || !tibco.admin.enterpriseAssets.shdResDefs.ShdResDefs) {
                        try {
                            tibco.admin.Main.loadClass("tibco/admin/enterpriseAssets/shdResDefs/ShdResDefs.js", "tibco.admin");
                            preLoad.add(tibco.admin.enterpriseAssets.shdResDefs.ShdResDefs, preLoad.size());
                        }
                        catch (ex) {
                            tibco.admin.Main.logException(ex);
                        }
                    }
                    else {
                        preLoad.add(tibco.admin.enterpriseAssets.shdResDefs.ShdResDefs, preLoad.size());
                    }

                    // Load User Management tab
                    if(!tibco.admin.enterpriseAssets.userMgt || !tibco.admin.enterpriseAssets.userMgt.UserMgtPlugin) {
                        try {
                            tibco.admin.Main.loadClass("tibco/admin/enterpriseAssets/userMgt/UserMgtPlugin.js", "tibco.admin");
                            preLoad.add(tibco.admin.enterpriseAssets.userMgt.UserMgtPlugin, preLoad.size());
                        }
                        catch (ex) {
                            tibco.admin.Main.logException(ex);
                        }
                    }
                    else {
                        preLoad.add(tibco.admin.enterpriseAssets.userMgt.UserMgtPlugin, preLoad.size());
                    }

                    // Load server registration tab
                    if(!tibco.admin.enterpriseAssets.sReg || !tibco.admin.enterpriseAssets.sReg.ServerRegPlugin) {
                        try {
                            tibco.admin.Main.loadClass("tibco/admin/enterpriseAssets/sReg/ServerRegPlugin.js", "tibco.admin");
                            preLoad.add(tibco.admin.enterpriseAssets.sReg.ServerRegPlugin, preLoad.size());
                        }
                        catch (ex) {
                            tibco.admin.Main.logException(ex);
                        }
                    }
                    else {
                        preLoad.add(tibco.admin.enterpriseAssets.sReg.ServerRegPlugin, preLoad.size());
                    }

                    //load keystore
                    if(!tibco.admin.enterpriseAssets.keyMgt || !tibco.admin.enterpriseAssets.keyMgt.Keys) {
                        try {
                            tibco.admin.Main.loadClass("tibco/admin/enterpriseAssets/keyMgt/Keys.js", "tibco.admin");
                            preLoad.add(tibco.admin.enterpriseAssets.keyMgt.Keys, preLoad.size());
                        }
                        catch(ex) {
                            tibco.admin.Main.logException(ex);
                        }
                    }
                    else {
                        preLoad.add(tibco.admin.enterpriseAssets.keyMgt.Keys, preLoad.size());
                    }

                    // Load admin servers tab
                    if(!tibco.admin.enterpriseAssets.servers || !tibco.admin.enterpriseAssets.servers.ServerPlugin) {
                        try {
                            tibco.admin.Main.loadClass("tibco/admin/enterpriseAssets/servers/ServerPlugin.js", "tibco.admin");
                            preLoad.add(tibco.admin.enterpriseAssets.servers.ServerPlugin, preLoad.size());
                        }
                        catch (ex) {
                            tibco.admin.Main.logException(ex);
                        }
                    }
                    else {
                        preLoad.add(tibco.admin.enterpriseAssets.servers.ServerPlugin, preLoad.size());
                    }

                    // load Substitution Variables
                    if(!tibco.admin.enterpriseAssets.SubVarsPlugin || !tibco.admin.enterpriseAssets.subVars.SubVarsPlugin) {
                        try {
                            tibco.admin.Main.loadClass("tibco/admin/enterpriseAssets/subVars/SubVarsPlugin.js", "tibco.admin");
                            preLoad.add(tibco.admin.enterpriseAssets.subVars.SubVarsPlugin, preLoad.size());
                        }
                        catch(ex) {
                            tibco.admin.Main.logException(ex);
                        }
                    }
                    else {
                        preLoad.add(tibco.admin.enterpriseAssets.subVars.SubVarsPlugin, preLoad.size());
                    }

                    this.loadPlugins("tibco.admin.enterpriseAssets.EnterpriseAssetsTabInterface", preLoad);
                }
                else {
                    this.onAfterGetPlugins();
                }
            }
        });
